﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MacomberMapSystem.Common.Database;
using System.Xml;
using MacomberMapSystem.Common.Serialization;
using System.IO;
using MacomberMapSystem.Common.CIM.Direct;
using System.ComponentModel;
using System.Drawing.Design;
using MacomberMapSystem.Common.User_Interfaces.Editors;

namespace MacomberMapSystem.Common.Processing
{
    /// <summary>
    /// This class is responsible for adding a new database to our repository
    /// </summary>
    public class CIM_AddToRepository: CIM_Processor
    {
        #region Variable declarations
        /// <summary>
        /// The alternate folder in which the model should be copied
        /// </summary>
        [MM_Serializable_Target(MM_Serializable_Target.enumTargetType.FieldValue), Category("Audits"), Description("The alternate folder in which the model should be copied"), Editor(typeof(MM_Folder_Browser), typeof(UITypeEditor))]
        public string AlternateFolder
        {
            get { return _AlternateFolder; }
            set { _AlternateFolder = value; }
        }
        private string _AlternateFolder;
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xElem"></param>
        /// <param name="NamedParameters"></param>
        /// <param name="Repository"></param>
        public CIM_AddToRepository(XmlElement xElem, Dictionary<String, Object> NamedParameters, MM_Repository Repository)
            : base(xElem, NamedParameters, Repository)
        {
        }

        /// <summary>Report the name of our process</summary>
        public override string ProcessName
        {
            get { return "Model to repository addition"; }
        }

        /// <summary>
        /// Run our process
        /// </summary>
        public override enumProcessState InitiateProcess(MM_Database_Connector DbConn)
        {            
            MM_Database_Model Model = NamedParameters["Model"] as MM_Database_Model;

            //If we haven't already, build an index of our model
            if (!File.Exists(Path.Combine(Model.ModelFullPath, Model.Name + ".SQLite")))
                (Model as CIM_DirectModel).WriteIndex(CIM_DirectModel.enumDatabaseMode.SQLite);


            //If we have an alternate folder, let's copy that over
            if (!String.IsNullOrEmpty(_AlternateFolder))
            {
                Model.AlternatePath = ProcessString(_AlternateFolder);
                if (!Directory.Exists(Model.AlternatePath))
                    Directory.CreateDirectory(Model.AlternatePath);
                foreach (string str in ".CimIndex,.xml,.SQLite".Split(','))
                    File.Copy(Path.Combine(Model.FullPath, Model.Name + str), Path.Combine(Model.AlternatePath, Model.Name + str));
            }
            
      
            //Write out our entry in the database                       
            Model.WriteRdb(DbConn.Repository);

            return enumProcessState.Completed;
        }
    }
}
